package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    // 分页查询广告信息
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllAdByPage(PromotionAdVO promotionAdVO) {

        PageInfo<PromotionAd> pageInfo = promotionAdService.findAllPromotionAdByPage(promotionAdVO);

        return new ResponseResult(true, 200, "分页查询广告信息成功", pageInfo);
    }

    // 图片上传
    @RequestMapping("/PromotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        // 1.判断接收到的上传文件是否为空
        if (file.isEmpty()) {
            throw new RuntimeException();
        }

        // 2.获取项目的部署路径
        String realpath = request.getServletContext().getRealPath("/");
        String substring = realpath.substring(0, realpath.indexOf("ssm-web"));

        // 3.获取原文件名
        String originalFilename = file.getOriginalFilename();

        // 4.生成新文件名
        String newFileName = System.currentTimeMillis() +
                originalFilename.substring(originalFilename.lastIndexOf("."));

        // 5.文件上传
        String uploadPath = substring + "upload\\";
        File filePath = new File(uploadPath, newFileName);

        // 如果目录不存在就创建目录
        if (!filePath.getParentFile().exists()) {
            filePath.getParentFile().mkdirs();
            System.out.println("创建目录：" + filePath);
        }

        // 进行真正的图片上传
        file.transferTo(filePath);

        // 6.将文件名和文件路径返回，进行响应
        Map<String, String> map = new HashMap<>();
        map.put("fileName", newFileName);
        map.put("filePath", "http://localhost:8080/upload/" + newFileName);

        return new ResponseResult(true, 200, "图片上传成功", map);
    }

    // 添加或修改广告信息
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {

        ResponseResult responseResult;
        if (promotionAd.getId() == null) {
            // 添加广告信息
            promotionAdService.savePromotionAd(promotionAd);
            responseResult = new ResponseResult(true, 200, "添加广告信息成功", null);
        } else {
            // 修改广告信息
            promotionAdService.updatePromotionAd(promotionAd);
            responseResult = new ResponseResult(true, 200, "修改广告信息成功", null);
        }
        return responseResult;
    }

    // 根据Id查询广告信息
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(int id) {

        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);

        return new ResponseResult(true, 200, "根据Id查询广告信息成功", promotionAd);
    }

    // 广告动态上下线
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(Integer id, Integer status) {

        promotionAdService.updatePromotionAdStatus(id, status);

        Map<String, Integer> map = new HashMap<>();
        map.put("status", promotionAdService.findPromotionAdById(id).getStatus());
        return new ResponseResult(true, 200, "广告动态上下线成功", map);
    }
}
